// -------------------------------------------------------------------------------------
// AskOperator
// -------------------------------------------------------------------------------------
// Permission is granted to freely redistribute this source code, and to use fragments
// of this code in your own applications if you find them to be useful.  This class,
// along with the source code, come with no warranty of any kind, and the user assumes
// all responsibility for its use.
// -------------------------------------------------------------------------------------

#import <appkit/appkit.h>
#import <libc.h>
#import <stdlib.h>
#import <c.h>
#import <sys/param.h>
#import <sys/types.h>
#import <sys/time.h>
#import "AskOperator.h"
#import "RemoteCommand.h"

// -------------------------------------------------------------------------------------
@implementation AskOperator

// -------------------------------------------------------------------------------------
// initialization
// -------------------------------------------------------------------------------------

+ new
{
	static	id	_self = (id)nil;
	if (!_self) _self = [[self alloc] init];
	return _self;
}

- init
{
	[super init];
	[NXApp loadNibSection:"AskOperator.nib" owner:self];
	return self;
}

// -------------------------------------------------------------------------------------
// view control
// -------------------------------------------------------------------------------------

- hideView:aView
{
	if ([aView isKindOf:[Button class]]) {
		[aView setEnabled:NO];
		[aView setTransparent:YES];
		[aView setTitle:""];
	} else
	if ([aView isKindOf:[TextField class]]) {
		[aView setStringValue:""];
		[aView setEnabled:NO];
		[aView setBezeled:NO];
		[aView setBackgroundGray:NX_LTGRAY];
		[aView setBackgroundTransparent:YES];
		[[aView superview] addSubview:self:NX_BELOW relativeTo:(id)nil];
	} else 
		printf("hideView: unsupported View type %s\n", [aView name]);
	return self;
}

- unhideView:aView title:(const char*)title
{
	if ([aView isKindOf:[Button class]]) {
		[aView setTitle:title];
		[aView setEnabled:YES];
		[aView setTransparent:NO];
	} else
	if ([aView isKindOf:[TextField class]]) {
		[aView setBackgroundTransparent:NO];
		[aView setStringValue:title];
		[aView setEnabled:YES];
		[aView setBezeled:YES];
		[aView setBackgroundGray:NX_WHITE];
		[[aView superview] addSubview:self:NX_ABOVE relativeTo:(id)nil];
	} else 
		printf("unhideView: unsupported View type %s\n", [aView name]);
	return self;
}

- fillView:aView title:(const char*)title dontHide:(BOOL)dontHide
{
	if ((title && *title) || dontHide) [self unhideView:aView title:title];
	else [self hideView:aView];
	return self;
}

// -------------------------------------------------------------------------------------
// message panels
// -------------------------------------------------------------------------------------

- buildAskPanel:(askOperator_t*)ao
{
	char	*dftButton1 = (ao->button[0] && *ao->button[0])? ao->button[0]: "Continue";
	[askPanel setTitle:ao->title];
	[askMessage setStringValue:ao->message];
	[self fillView:askText    title:ao->text      dontHide:(ao->type == 1)];
	[self fillView:askButton1 title:dftButton1    dontHide:NO ];
	[self fillView:askButton2 title:ao->button[1] dontHide:NO ];
	[self fillView:askButton3 title:ao->button[2] dontHide:NO ];
	[self fillView:askButton4 title:ao->button[3] dontHide:NO ];
	return self;
}

+ (const char*)showAskPanel:(askOperator_t*)ao
{
	self = [self new];
	[self buildAskPanel:ao];
	textResponse = (const char*)nil;
	buttonResponse = (const char*)nil;
	[askPanel center];
	[askPanel display];
	[askPanel makeKeyAndOrderFront:(id)nil];
	[NXApp runModalFor:askPanel];
	[askPanel orderOut:(id)nil];
	return (ao->type == 0)? buttonResponse : textResponse;
}

// -------------------------------------------------------------------------------------
// operator responses
// -------------------------------------------------------------------------------------

- askButtonResponse:sender
{
	buttonResponse = [sender title];
	textResponse = [askText isEnabled]? [askText stringValue] : (const char*)nil;
	[NXApp abortModal];
	return self;
}

@end
